<?php

namespace utils\wechat;
class DevTool 
{
    private $port;
    private $installPath = 'C:\\Program Files (x86)\\Tencent\\微信web开发者工具';
    private $userDataPath = 'C:\\Users\\Administrator\\AppData\\Local\\微信开发者工具\\User Data';
    private $projectPath = "C:\\mp-weixin\\";
    private $logPath = "C:\\log\\upload.log";
    private $host;
    private $apiUrl;
    private $appid;
    private $version;
    private $entoken;
    private $versionCode = [
        'wechatopen' => ['desc' => '订货猫开放平台版本','version' => '1.0.0'], 
        'independent' => ['desc' => '订货猫独立版','version' => '1.0.0'],
        'merchant' => ['desc' => '订货虎','version' => '1.0.0'],
        'share' => ['desc' => '免审核版本','version' => '1.0.0']
    ];
    function __construct($initVersion,$entoken = ''){
        $version = $this->getVersion();
        $this->userDataPath = $this->userDataPath.'\\'.md5($this->installPath.$version['latestNw']);
        $this->port = $this->getPort();
        $this->host = 'http://127.0.0.1:'.$this->port.'/v2';
        $this->apiUrl = request()->param('apiUrl');
        $this->appid = request()->param('appid');
        $this->version = $initVersion;
        $this->entoken = $entoken;
    }
    public function login(){
        $response = curl_get($this->host.'/login');
        if ( $response ){
            return 'data:image/jpeg;base64,'.base64_encode($response);
        }
        throw new \Exception('获取登录二维码失败');
    }
    public function upload(){
        $this->setProjectConfigJson();
        if ( $this->version == 'wechatopen' ){
            $this->setExtJson();
        }
        if ( $this->version != 'wechatopen' ){
            $this->setApiUrl();
        }
        $response = curl_get($this->host.'/upload',['project' => urlencode($this->projectPath.$this->version),'version' => $this->versionCode[$this->version]['code'],'desc' => urlencode($this->versionCode[$this->version['desc']]),'info-output' => urlencode($this->logPath)]);
        
        return json_decode( $response,true) ;
    }
    public function preview(){
         $response = curl_get($this->host.'/preview',['project' => urlencode($this->projectPath.$this->$version)]);
         echo $response;
    }
    public function isLogin(){
        $response = curl_get($this->host.'/isLogin');
        return json_decode($response,true);
    }
    private function getPort(){
        $portFile = $this->userDataPath.'\\Default\.ide';
        if ( !file_exists($portFile) ){
             throw new \Exception('端口文件不存在');
        }
        return file_get_contents($portFile);
    }
    private function getVersion(){
        if ( !file_exists($this->installPath.'\\version') ){
            throw new \Exception('版本文件不存在');
        }
        $version = file_get_contents($this->installPath.'\\version');
        return json_decode($version,true);
    }
    private function setApiUrl(){
        $file =  $this->projectPath.$this->version.'\\common\\.vendor.js';
        if ( !file_exists($file) ){
            throw new \Exception($file.'文件不存在');
        }
        $file = file_get_contents($file);
        $file = str_replace("{{apiUrl}}",$this->apiUrl,$file);
        $file = str_replace("{{token}}",$this->entoken,$file);
         file_put_contents($this->projectPath.$this->version.'.\\common\\vendor.js',$file);
    }
    private function setExtJson(){
        $jsonFile = $this->projectPath.$this->version.'.\\.ext.json';
        if ( !file_exists($jsonFile) ){
            throw new \Exception($jsonFile.'文件不存在');
        }
        $json = file_get_contents($jsonFile);
        $json = json_decode($json,true);
        $json['ext']['apiUrl'] = $this->apiUrl;
        $json['extAppid'] = $this->appid;
        $json = json_encode($json,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        file_put_contents($this->projectPath.$this->version.'\\ext.json',$json);
    }
    private function setProjectConfigJson(){
         $jsonFile = $this->projectPath.$this->version.'\\.project.config.json';
        if ( !file_exists($jsonFile) ){
            throw new \Exception($jsonFile.'文件不存在');
        }
        $json = file_get_contents($jsonFile);
        $json = json_decode($json,true);
 
        $json['appid'] = $this->appid;
        $json['packOptions']['ignore'][] = ['type' => 'file','value' => 'common/.vendor.js'];
        $json['packOptions']['ignore'][] = ['type' => 'file','value' => '.project.config.json'];
        $json = json_encode($json,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        file_put_contents($this->projectPath.$this->version.'\\project.config.json',$json);
    }
    
}
